package MineGod;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.geom.Rectangle;

public class EntitySand extends Entity {

	public double durability;
	public double maxDurability;
	public double y;
	public double ySpeed = .15;
	
	public EntitySand(double x, double y, double dur){
		durability = dur;
		maxDurability = 50;
		this.x = x;
		this.y = y;
		
		dead = false;
		
		mask = new Rectangle((float)(x), (float)(y), (float)BlockSand.WIDTH, (float)BlockSand.WIDTH);
	}
	@Override
	public void interactWithPlayerOnCollision(Player player) {
		player.damage(10, DamageType.sand);
	}

	@Override
	public void doLogic(int delta, Chunk prevChunk, Chunk currChunk, Chunk nextChunk) {
		// TODO Auto-generated method stub
		y = y+ySpeed*delta;
		mask = new Rectangle((float)(x), (float)(y), (float)BlockSand.WIDTH, (float)BlockSand.WIDTH);
		Block b2 = Utils.pointToBlock(x, y+16);
		if ((b2==null) || (b2.type != BlockType.air && b2.type != BlockType.water)){
			//Change current block to block sand
			Block b = new BlockSand();
			b.durability = durability;
			Utils.changeCurrentBlock(b, x, y);
			dead = true;
		}
		
		
	}

	@Override
	public void render(double worldPos) {
		if (!(x+Block.WIDTH-worldPos<0 || x-Block.WIDTH-worldPos>Chunk.CHUNK_WIDTH*Block.WIDTH)){
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
			BlockSand.sandTex.bind();
			double renderX = x - worldPos;
			GL11.glColor3d((durability)/maxDurability, (durability)/maxDurability, (durability)/maxDurability); // Could make this faster!!
			GL11.glBegin(GL11.GL_QUADS);
				GL11.glNormal3d(0, 0, 1);
				GL11.glTexCoord2d(0.0, 0.0);
				GL11.glVertex3d(renderX, y, zIndex);
				GL11.glTexCoord2d(1.0, 0.0);
				GL11.glVertex3d(renderX + BlockSand.WIDTH, y, zIndex);
				GL11.glTexCoord2d(1.0, 1.0);
				GL11.glVertex3d(renderX + BlockSand.WIDTH, y + BlockSand.WIDTH, zIndex);
				GL11.glTexCoord2d(0.0, 1.0);
				GL11.glVertex3d(renderX, y + BlockSand.WIDTH, zIndex);
			GL11.glEnd();
		}
	}

}
